package com.derbysoft.dswitch.adapter.ihg.common.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

public abstract class EnvironmentUtils {

    private static Properties environmentMappings = new Properties();

    private static transient Log logger = LogFactory.getLog(EnvironmentUtils.class);

    static {
        loadMappings();
    }

    public static String get(String key) {
        return environmentMappings.getProperty(key);
    }

    public static int getMaxConnections() {
        return Integer.parseInt(get("http.connection-manager.max-total"));
    }

    public static int getAvailConnectionTimeout() {
        return Integer.parseInt(get("availability.http.connection.timeout"));
    }

    public static int getAvailReadTimeout() {
        return Integer.parseInt(get("availability.http.socket.timeout"));
    }

    public static int getReservationConnectionTimeout() {
        return Integer.parseInt(get("reservation.http.connection.timeout"));
    }

    public static int getReservationReadTimeout() {
        return Integer.parseInt(get("reservation.http.socket.timeout"));
    }

    public static String getGlobalIhgAvailabilityUrl() {
        return get("ihg.availability.service.url");
    }

    public static String getIhgReservationUrl() {
        return get("ihg.book.reservation.service.url");
    }

    public static String getIhgCancelUrl() {
        return get("ihg.cancel.reservation.service.url");
    }

    public static String getIhgAvailabilityRequestType() {
        return get("ihg.availability.request.type");
    }

    public static String getIhgReservationResponseType() {
        return get("ihg.reservation.response.type");
    }

    public static boolean appendDerbyLog() {
        return Boolean.valueOf(get("append.derby.log"));
    }

    private static void loadMappings() {
        try {
            environmentMappings = PropertiesLoaderUtils.loadAllProperties("environment.properties");
        } catch (IOException e) {
            String errorMsg = "load environment.properties sufferd error !!";
            if (logger.isErrorEnabled()) {
                logger.error(errorMsg, e);
            }
            throw new RuntimeException(errorMsg);
        }
    }
}